package com.anmi.transfer.table.concrete;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.anmi.alfred.entity.TransferCaseOperationResult;
import com.anmi.alfred.service.concreteservice.CaseOperationIndexService;
import com.anmi.transfer.service.TransferCaseOperationService;
import com.anmi.transfer.table.base.AbstractSyncTable;
import com.anmi.transfer.util.SpringContextUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CaseOperationSync extends AbstractSyncTable {

  {
    TABLE_NAME = "case_operation";
  }

  @Override
  public void handleData(List<CanalEntry.Entry> entrys) {
    super.handleData(entrys);
  }

  @Override
  public void insert(CanalEntry.Column column) {
    TransferCaseOperationService caseOperationService =
        SpringContextUtil.getBean(TransferCaseOperationService.class);
    TransferCaseOperationResult caseOperationResult =
        caseOperationService.selectByCaseOperationId(Long.valueOf(column.getValue()));
    if (caseOperationResult != null) {
      String json =
          JSON.toJSONString(caseOperationResult, SerializerFeature.WriteNullStringAsEmpty);
      logger.info("索引的文档JSON格式数据为{}", json);
      CaseOperationIndexService caseOperationIndexService =
          SpringContextUtil.getBean(CaseOperationIndexService.class);
      caseOperationIndexService.index(
          TransferCaseOperationResult.ALIAS_NAME, caseOperationResult.getId().toString(), json);
    }
  }

  @Override
  public void update(CanalEntry.Column column) {
    this.insert(column);
  }
}
